package org.bot.server.job;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.bot.server.flow.mapper.BotFlowMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @Author: 沧月
 * @Date: 2023/12/9 12:06
 * @Description: 描述
 */

@Component
public class FlowQuartzJob {

    @Autowired
    BotFlowMapper flowMapper;

    @Autowired
    QuartzJob quartzJob;

    public void addClientJob(String clientId) {
        JSONArray list = new JSONArray(flowMapper.jobClientTask(clientId));
        list.stream().filter(map -> this.addJobCondition((Map<String, Object>) map)).forEach(map ->
                this.addJob((Map<String, Object>) map));
    }

    public void addFlowJob(String flowId) {
        Map<String, Object> map = flowMapper.jobFlowTask(flowId);
        if (this.addJobCondition(map)) {
            this.addJob(map);
        }
    }

    private boolean addJobCondition(Map<String, Object> map) {
        JSONObject _tmp = new JSONObject(map);
        return _tmp.getIntValue("client_status") == 2 && _tmp.getIntValue("status") == 1
                && _tmp.getIntValue("flow_type") == 2 && _tmp.getString("cron") != null;
    }

    private void addJob(Map<String, Object> list) {
        if (list == null || list.isEmpty()) {
            return;
        }
        JSONObject item = new JSONObject(list);
        JobEntity jobEntity = new JobEntity();
        String triggerKey = String.format("task_trigger_%s", item.getString("id"));
        jobEntity.setTriggerKey(triggerKey);
        jobEntity.setTriggerGroup("task_trigger_group");
        jobEntity.setCron(item.getString("cron"));
        jobEntity.setJobKey(String.format("job_%s", item.getString("id")));
        jobEntity.setJobGroup("flowJob");
        jobEntity.setJobClass(FlowJob.class);

        jobEntity.putData("flowId",item.getString("id"));
        quartzJob.addJob(jobEntity);
    }


    public void deleteClientJob(String clientId) {
        JSONArray list = new JSONArray(flowMapper.jobClientTask(clientId));
        list.forEach(map -> {
            this.deleteJob((Map<String, Object>) map);
        });
    }


    public void deleteFlowJob(String flowId) {
        Map<String, Object> map = flowMapper.jobFlowTask(flowId);
        this.deleteJob(map);
    }


    private void deleteJob(Map<String, Object> list) {
        if (list == null || list.isEmpty()) {
            return;
        }
        JSONObject item = new JSONObject(list);
        JobEntity jobEntity = new JobEntity();
        jobEntity.setJobKey(String.format("job_%s", item.getString("id")));
        jobEntity.setJobGroup("flowJob");
        String triggerKey = String.format("task_trigger_%s", item.getString("id"));
        jobEntity.setTriggerKey(triggerKey);
        jobEntity.setTriggerGroup("task_trigger_group");
        quartzJob.deleteJob(jobEntity);
    }


}
